/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.boudreaulocke.web.pages;

import java.util.Iterator;

import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.ISortState;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.ISortStateLocator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.HeadersToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.NavigationToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.util.SingleSortState;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.boudreaulocke.model.persistence.PersistentPerson;
import com.boudreaulocke.model.persistence.IPersonDao;
import com.boudreaulocke.web.model.PersistentPersonModel;
import com.locke.library.persistence.dao.query.clauses.Ascending;
import com.locke.library.persistence.dao.query.clauses.Descending;
import com.locke.library.persistence.dao.query.clauses.Range;
import com.locke.library.persistence.dao.query.clauses.SortOrder;

/**
 * Home page for JavaOne demo
 */
public class People extends BaseWebPage {

	private static final long serialVersionUID = 1L;

	@SpringBean(name = "personDao")
	private IPersonDao personDao;

	// Sort state for table
	private SingleSortState sortState = new SingleSortState();

	/**
	 * Construct.
	 * 
	 * @param parameters
	 *            Page parameters
	 */
	@SuppressWarnings("unchecked")
	public People(final PageParameters parameters) {

		sortState.setSort(new SortParam("name", true));

		final IColumn<PersistentPerson>[] columns = new IColumn[3];
		columns[0] = new PropertyColumn<PersistentPerson>(new Model<String>(
				getString("nameColumnTitle")), "name", "name");
		columns[1] = new PropertyColumn<PersistentPerson>(new Model<String>(
				getString("phoneNumberColumnTitle")), "phoneNumber",
				"phoneNumber");
		columns[2] = new AbstractColumn<PersistentPerson>(new Model<String>(
				getString("actionColumnTitle"))) {
			private static final long serialVersionUID = 8978713409296092663L;

			public void populateItem(
					Item<ICellPopulator<PersistentPerson>> cellItem,
					String componentId, final IModel<PersistentPerson> rowModel) {
				cellItem.add(new ActionPanel(componentId,
						new PersistentPersonModel(rowModel)));
			}
		};

		SortableDataProvider<PersistentPerson> provider = new SortableDataProvider<PersistentPerson>() {

			private static final long serialVersionUID = 3747240521947721992L;

			public Iterator<? extends PersistentPerson> iterator(int first,
					int count) {
				SortParam sort = sortState.getSort();
				SortOrder order = (sort.isAscending() ? new Ascending(sort
						.getProperty()) : new Descending(sort.getProperty()));
				return personDao.query(new Range(first, count), order).matches()
						.iterator();
			}

			public IModel<PersistentPerson> model(PersistentPerson object) {
				return new PersistentPersonModel(object);
			}

			public int size() {
				return (int) personDao.query().countMatches();
			}
		};

		DataTable<PersistentPerson> table = new DataTable<PersistentPerson>(
				"table", columns, provider, 15) {

			private static final long serialVersionUID = 2329013181331271749L;

			@Override
			protected Item newRowItem(String id, int index, IModel model) {
				Item item = super.newRowItem(id, index, model);
				item.add(new SimpleAttributeModifier("class",
						(index % 2 == 0) ? "even" : "odd"));
				return item;
			}
		};
		table.addBottomToolbar(new NavigationToolbar(table));
		table.addTopToolbar(new HeadersToolbar(table, new ISortStateLocator() {

			private static final long serialVersionUID = 1L;

			public ISortState getSortState() {
				return sortState;
			}

			public void setSortState(final ISortState state) {
				sortState = (SingleSortState) state;
			}
		}));

		add(table);
		add(new Link<Void>("create") {

			private static final long serialVersionUID = -14444412090394296L;

			@Override
			public void onClick() {
				setResponsePage(new EditPerson(new PersistentPersonModel(
						new PersistentPerson())));
			}
		});
	}

	private class ActionPanel extends Panel {

		private static final long serialVersionUID = 9104308432272224302L;

		public ActionPanel(String id, final PersistentPersonModel model) {
			super(id);
			add(new Link<Void>("edit") {
				private static final long serialVersionUID = -6939696243524179515L;

				@Override
				public void onClick() {
					setResponsePage(new EditPerson(model));
				}
			});
			add(new Link<Void>("delete") {
				private static final long serialVersionUID = -6939696243524179515L;

				@Override
				public void onClick() {
					model.delete();
				}
			});
		}
	}
}
